<template>
  <view class="search-detail-view">
    <!--  搜索框  -->
    <fixed-search
        :search-name="curSearchName"
        ipt-placeholder="请输入姓名关键字"
        @arrow-click="goHonourPage"
        @handle-search-icon-click="handleSearch"
    />
    <view class="filter">
      <cell-group title="高级筛选" :cell-list="cellList" @handle-popup-cell-click="getSelectVal"/>
    </view>
    <view class="top-search">
      <view class="top-search-title">
        <h3>热门搜索</h3>
      </view>
      <view class="top-search-content">
        <text class="top-search-content-item"
              v-for="item in topSearchList" :key="item.id" @tap="handleTopSearchItemClick(item)">
          {{ item.filedName }}
        </text>
      </view>
    </view>
  </view>
</template>
<script setup>
import fixedSearch from './components/fixed-search.vue'
import cellGroup from './components/cell-group.vue'
import {onLoad} from "@dcloudio/uni-app";
import {inject, ref, defineProps, nextTick} from "vue";
import {generateRandomNumId, getUrlParamsInH5, getUrlParamsInWeixin} from "@/utils/utils";
import {
  getConfigEnterpriseFieldsApi,
  getHonourYearsListApi,
  getTopSearchApi
} from "@/api/honour/search";
import {customNav} from "@/utils/customNav";

const props = defineProps({
  searchType: {
    type: String,
    default: '企业荣誉'
  }
})
const cellList = ref([])

/**
 * 监听筛选单元格点击事件
 * @param val
 */
const getSelectVal = val => {
  console.log(val);
  const {labelName, selectedItem} = val
  for (const cell of cellList.value) {
    if (cell.filedName === labelName) {
      for (const child of cell.children) {
        if (child.filedName === selectedItem.filedName) {
          cell.value = selectedItem.filedName
          child.checked = true;
          return;
        }
      }
    }
  }
}

const curSearchType = ref(0)
const curSearchName = ref('')
const searchTypeDict = {
  '企业荣誉': 0,
  '集体荣誉': 1,
  '个人荣誉': 2
}
/**
 * 获取企业高级筛选内容
 * @returns {Promise<void>}
 */
const getConfigEnterpriseFields = async () => {
  const params = {
    type: searchTypeDict[curSearchType.value] || searchTypeDict['企业荣誉']
  }
  const res = await getConfigEnterpriseFieldsApi(params)
  const {code, data} = res
  if (code === 200) {
    cellList.value =   data.map(item => {
      item.children.unshift({
        id: generateRandomNumId(19),
        filedName: '不限'
      })
    })
    
    console.log("data",data);
    await getHonourYearsList(data)
  }
}
/**
 * 获取荣誉年份数据
 * @param config
 * @returns {Promise<void>}
 */
const getHonourYearsList = async (config) => {
  const res = await getHonourYearsListApi()
  const {code, data} = res
  if (code === 200) {
    const {yearList} = data || {}
    const treatedYearList = yearList.map(item => {
      return {
        id: new Date().getTime() + item,
        filedName: item,
      }
    })
    // config.push({
    //   id: new Date().getTime(),
    //   filedName: '荣誉年份',
    //   children: [{
    //     id: generateRandomNumId(19),
    //     filedName: '不限'
    //   }].concat(treatedYearList)
    // })
    Array.isArray(config) && config.forEach(item => {
      item.cellType = 'select'
      item.value = '不限'
    })
    // config.pop()
    cellList.value = config
    console.log("cellList.value",cellList.value);
  }
}

// 热搜榜单
const topSearchList = ref([])
const getTopSearchList = async () => {
  const res = await getTopSearchApi("2")
  const {code, data} = res
  if (code === 200) {
    topSearchList.value = data
  }
}

/**
 * 处理搜索数据
 * @param searchIptVal
 */
const handleSearch = searchIptVal => {
  console.log("cellList.value",cellList.value);
  // 学历分类
  const honourLevelData = cellList.value.find(cell => cell.filedName === '学历分类')
  let honourLevelVal = honourLevelData?.children.find(child => child.checked)?.filedName
  honourLevelVal = honourLevelVal === '不限' ? '' : honourLevelVal
  // 职称层级
  const honourArea = cellList.value.find(cell => cell.filedName === '职称层级')
  let honourAreaVal = honourArea?.children.find(child => child.checked)?.filedName
  honourAreaVal = honourAreaVal === '不限' ? '' : honourAreaVal

  // // 荣誉年份
  // const honourYear = cellList.value.find(cell => cell.filedName === '荣誉年份')
  // let honourYearVal = honourYear?.children.find(child => child.checked)?.filedName
  // honourYearVal = honourYearVal === '不限' ? '' : honourYearVal

  const params = {
    realName: searchIptVal,
    // yearList: honourYearVal ? [honourYearVal] : [],
    educationList: honourLevelVal ? [honourLevelVal] : [],
    jobTitleList: honourAreaVal ? [honourAreaVal] : []
  }
  console.log(params);
  getEnterPriseList(params)
}

const handleTopSearchItemClick = item => {
  nextTick(() => {
    curSearchName.value = item.filedName
  })
}
const packageHonourPath = inject('packageHonourPath')

const getEnterPriseList = params => {
  customNav(`${packageHonourPath}/personal-list?searchParams=${JSON.stringify(params)}`)
}

const goHonourPage = () => {
  customNav(`${packageHonourPath}/index`)
}

onLoad(() => {
  /* #ifndef MP-WEIXIN */
  curSearchType.value = getUrlParamsInH5()?.searchType
  curSearchName.value = getUrlParamsInH5()?.searchName
  /* #endif */

  /* #ifdef MP-WEIXIN */
  curSearchType.value = getUrlParamsInWeixin()?.searchType
  curSearchName.value = getUrlParamsInWeixin()?.searchName
  /* #endif */
  getConfigEnterpriseFields()
  getTopSearchList()
})
</script>
<style scoped lang=less>
.search-detail-view {
  .filter {
    margin-top: 50rpx;
  }

  .top-search {
    padding: 0 40rpx;
    margin-top: 50rpx;

    &-title {
      h3 {
        font-size: 31rpx;
        font-weight: 500;
        color: #272636;
      }
    }

    &-content {
      display: flex;
      flex-wrap: wrap;

      &-item {
        display: inline-block;
        height: 54rpx;
        line-height: 54rpx;
        background-color: #ffffff;
        font-size: 23rpx;
        font-family: 'ali';
        text-align: center;
        color: #68696a;
        margin-top: 20rpx;
        margin-right: 20rpx;
        padding: 0 45rpx;
        border-radius: 7rpx;
      }
    }
  }
}
</style>
